package componentes;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class labtext extends JPanel implements ActionListener{

	
	int tipo,min,max,i;
	GridBagConstraints gbc;
	private JLabel label;
	private JTextField text;
	private JLabel bicono;
	 ImageIcon icono,ibien,imal;
String cadena;
	
public labtext(String cadena,int tipo,int min,int max){

	this.min = min;
	this.max=max;
	if(tipo==5){
		this.text = new JPasswordField("",10);
	}else{
	text = new JTextField("",10);
	}
	this.dibujar(cadena);	
	this.setTipo(tipo);
	
}
	
public labtext(String cadena){
	this.tipo = 1;
	this.min = 3;
	this.max=15;
	text = new JTextField("",10);
	this.dibujar(cadena);
	
}

public void dibujar(String cadena){
	
	try{
	    icono = new ImageIcon(getClass().getResource("/recursos/cerrar.png/"));
		Image img = icono.getImage();  
	     Image newimg = img.getScaledInstance(16, 16,  java.awt.Image.SCALE_SMOOTH);  
	     ImageIcon newIcon = new ImageIcon(newimg); 
	     imal=newIcon;  
	     icono = new ImageIcon(getClass().getResource("/recursos/s_success.png/"));
	 	 img = icono.getImage();  
	       newimg = img.getScaledInstance(16, 16,  java.awt.Image.SCALE_SMOOTH);  
	       newIcon = new ImageIcon(newimg); 
	      ibien=newIcon;
		}catch(Exception e){
			
			
		}
	     
	     
	     
	     
		label = new JLabel(cadena);
		
		label.setToolTipText(cadena);
		bicono = new JLabel();
		bicono.setSize(15,15);
		bicono.setPreferredSize(new Dimension(17, 17));
		bicono.setIcon(imal );
		bicono.setToolTipText( "Tienes que introducir mas de 2 letras" );    
		text.addActionListener(this);
		text.addFocusListener (new FocusListener()
		{
		@Override
		public void focusGained(FocusEvent arg0) {
			// TODO Auto-generated method stub	
		}
		@Override
		public void focusLost(FocusEvent arg0) {
			// TODO Auto-generated method stub
		validar();
		}
		});	
		this.setLayout(new GridBagLayout());
		  gbc = new GridBagConstraints(0, 0, 1, 1, 2, 1,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
							0, 0, 0, 0), 0, 0);
			this.add(label, gbc);
		  gbc = new GridBagConstraints(1, 0, 1, 1, 0, 0,
						GridBagConstraints.CENTER, GridBagConstraints.WEST, new Insets(
								0, 0, 0, 0), 0, 0);
				this.add(text, gbc);
		  gbc = new GridBagConstraints(4, 0, 1, 1, 0, 0,
							GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(
									0, 0, 0, 0), 0, 0);
				this.add(bicono, gbc);			
	
}

public int getTipo() {
	return tipo;
}

public void setTipo(int tipo) {
	this.tipo = tipo;
	if(tipo==0){
		bicono.setToolTipText( "Tienes que introducir caracteres" ); 
	}
	
if(tipo==1){	
	bicono.setToolTipText( "Tienes que introducir  letras" ); 	
	}

if(tipo==2){
	bicono.setToolTipText( "Tienes que introducir numeros" ); 	
}

if(tipo==3){
	bicono.setToolTipText( "Tienes que introducir un DNI valido \n Formato: NNNNNNNN-L" ); 	
}

if(tipo==4){
	this.setMin(8);
	bicono.setToolTipText( "Tienes que introducir un numero de telefono" );
	this.setMax(11);
}
if(tipo==4){
	this.setMin(3);
	bicono.setToolTipText( "Tienes que introducir entre 3 y 20 caracteres" );
	this.setMax(20);
}


	
}

public int getMin() {
	return min;
}

public void setMin(int min) {
	this.min = min;
}

public int getMax() {
	return max;
}

public void setMax(int max) {
	this.max = max;
}

public String getText() {
	return text.getText();
}

public void setText(String cadena) {
	this.text.setText(cadena);
	this.validar();
}
	
@SuppressWarnings("unused")
public boolean validar(){
	cadena = text.getText();
	//solo mira el numero de caracteres
	if (tipo==0){
		if ((cadena.length() >= min)&&(cadena.length()<=max)){
			bicono.setIcon(ibien);
	       	bicono.setToolTipText( "Excelente!" );
		return true;
		}else{
			 bicono.setIcon(imal);
			 bicono.setToolTipText( "Tienes que introducir entre " + min + " y " + max + " caracteres");
			return false;
		}
	}

	//para cadenas de texto
	if (tipo ==1){
	if ((cadena.length() >= min)&&(cadena.length()<=max)){
		cadena = null;
		bicono.setIcon(ibien);
		bicono.setToolTipText( "Excelente!" );
		return true;
	}else{
		bicono.setIcon(imal);
		bicono.setToolTipText( "Tienes que introducir entre " + min + " y " + max + " caracteres");
		cadena = null;
		return false;
	}
	}
	
	
	//para numericos sin decimales
	if(tipo==2){
		try {
			cadena = text.getText();
			if ((cadena.length() >= min)&&(cadena.length()<=max)){
		   int valor = Integer.parseInt(cadena);
		    bicono.setIcon(ibien);
	       	bicono.setToolTipText( "Excelente!" );
		    return true;  
			}
		} catch (NumberFormatException nfe){
		    bicono.setIcon(imal);
		  	bicono.setToolTipText( "Tienes que introducir numeros entre " + 0 + " y " + (((int)Math.pow(10 , max))*10 -1) );
		  	cadena = null;
		  	return false;		      
        }
		return false;
	}
	
	//Valida un DNI
	if(tipo ==3){
		try{
			cadena = text.getText();
			if(nif(cadena)==true){
			    bicono.setIcon(ibien);
		       	bicono.setToolTipText( "Excelente!" );
			    return true; 
			 }
			    bicono.setIcon(imal);
			  	bicono.setToolTipText( "Tienes que introducir un DNI valido: NNNNNNNN-L");
			  	cadena = null;
			  	return false;
		} catch (Exception e) {
			e.printStackTrace();
		    bicono.setIcon(imal);
		  	bicono.setToolTipText( "Tienes que introducir un DNI valido: NNNNNNNN-L   Exception!!!");
		  	cadena = null;
		  	return false;
		}	
	}
	
	//telefonos
	if(tipo==4){
		try {
			if ((cadena.length() >= min)&&(cadena.length()<=max)){
			cadena = text.getText();
		    int valor = Integer.parseInt(cadena);
		    bicono.setIcon(ibien);
	       	bicono.setToolTipText( "Excelente!" );
		    return true;   
			}
			bicono.setIcon(imal);
		  	bicono.setToolTipText( "Tienes que introducir un numero de telefono" );
		  	cadena = null;
		  	return false;	
		} catch (NumberFormatException nfe){
		    bicono.setIcon(imal);
		  	bicono.setToolTipText( "Tienes que introducir un numero de telefono" );
		  	cadena = null;
		  	return false;		      
        }	
	}
	
	if (tipo==5){
		if ((cadena.length() >= min)&&(cadena.length()<=max)){
			bicono.setIcon(ibien);
	       	bicono.setToolTipText( "Excelente!" );
		return true;
		}else{
			 bicono.setIcon(imal);
			 bicono.setToolTipText( "Tienes que introducir entre " + min + " y " + max + " caracteres");
			return false;
		}
	}
	
return false;
}

@Override
public void actionPerformed(ActionEvent e) {
	 validar();
}

//funcion para validar el DNI
public boolean nif(String n) throws Exception{

 

Pattern patron=Pattern.compile("\\d{3,8}\\-[azA-Z]{1}");

Matcher m=patron.matcher(n);

if( m.matches()){

if( this.validarLetra(n))

return true;

}

return false;
}

 
//funcion para validar la letra del DNI
private boolean validarLetra(String n){



String[] nif=n.split("-");

 

int letra=(Integer.valueOf(nif[0]).intValue())%23;

String[] abc={"T","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E","T"};
nif[1].toUpperCase();


if(abc[letra].compareToIgnoreCase(nif[1])==0){
return true;
}
return false;
}


public void setEditable(boolean bol){
	
	this.text.setEditable(bol);
}
}


	
	
	

